var fieldReadonly=[];
var fieldAccess     = {"id":fieldId,"readonly":fieldReadonly};
var fieldAccessList = [];  
var fieldId = {"workflowName.workflowName":workflowName,"stepId":-1};
$(function(){
	 //保存当前所选步骤的name禁用字段
	$("#stepInfos").selectable({
           selected:function(event,ui){
		   var sobj = ui['selected'];
           var stepId =  sobj.attributes['stepId']['value'];
           var stepName = sobj.attributes['stepName']['value'];
           fieldId.stepId = stepId;
           fieldId.stepName= stepName;
           var fid = "fieldId.stepId="+stepId
           +"&fieldId.workflowName="+workflowName
           +"&fieldId.stepName="+stepName;
           $(".fieldArea").block({"message":"正在加载..."});
           $.ajax({
                url:"admin/field/field-manager!readonly",
                data:fid,
                type:"POST",
                success:function(datas){
                     renderForm(formField,datas);
                     $(".fieldArea").unblock();
                },
                error:function(msg){
                    $("#debug").html(msg);
                    $(".fieldArea").unblock();
                }
           });
           }            
       });
	 $("#savefeild").click(function(){
		 saveOne();
	 }).button();
	 var checkall = true;
	 $("#enableAll").bind("click",function(){
		 
		 $("#eleCon :checkbox").attr('checked',checkall);
		 checkall = !checkall;
	 });	 
	
});

function renderForm(formField,readonlys){
    var tbody = $("#eleCon");
    tbody.empty();
    var i=0;
    $.each(formField,function(i,node){
      if(i%5==0){
      	   tbody.append("<tr>");
      }
       var chkbox = $("<input type='checkbox'></input>");
       var label = $("<label class='fieldlabel'>"+node.name+"</label>");
       label.prepend(chkbox);
       chkbox.attr({
            "name":node.name
       });
       var td = $("<td></td>").addClass("ftd");
       if($.inArray(node.name,readonlys)!=-1){
            chkbox.attr("checked",true);
            td.addClass("fieldDisabled");
       }
             
       td.append(label);
       tbody.append(td);      
       if(i%4==0){
    	   tbody.append("</tr>");
       }
	  }); 

}

function saveOne(){
	 var params = "fieldAccess.id.workflowName="+workflowName;
    params+="&fieldAccess.id.stepId="+fieldId.stepId;
	 params+="&fieldAccess.id.stepName="+fieldId.stepName;
    $.each($("form").serializeArray(),function(i,node){
        params+="&fieldAccess.readonly="+node.name;       
    });
    var url="admin/field/field-manager!add?mapDefaultActionMethod=false";
    doSubmit(url,params);   
 }